package com.mingtu.dzpatrol.activity;

import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.KeyEvent;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.FrameLayout;

import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.AdaptScreenUtils;
import com.blankj.utilcode.util.DeviceUtils;
import com.blankj.utilcode.util.SPStaticUtils;
import com.blankj.utilcode.util.StringUtils;
import com.gyf.immersionbar.ImmersionBar;
import com.mingtu.dzpatrol.R;
import com.mingtu.dzpatrol.api.AppConfig;
import com.mingtu.dzpatrol.base.BaseApplication;
import com.mingtu.dzpatrol.manager.ScreenManager;
import com.mingtu.dzpatrol.utils.ActivityUtil;
import com.mingtu.dzpatrol.utils.IntentUtils;
import com.mingtu.dzpatrol.utils.SPTools;
import com.mingtu.dzpatrol.utils.ScreenUtils;


public class WelcomeActivity extends AppCompatActivity {
    protected static final long ANIMATION_DELAY = 1000;
    private FrameLayout layoutWelcome;
    protected boolean isStatusBar = false;//是否沉浸状态栏
    protected boolean isFullScreen = false;//是否允许全屏
    protected boolean isScreenPortrait = true;//是否禁止旋转屏幕

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initScreenAdaption();
        setContentView(R.layout.activity_welcome);
        layoutWelcome = findViewById(R.id.layout_welcome);
        // 添加到Activity工具类
        ActivityUtil.getInstance().addActivity(this);
        initView();
    }

    @Override
    protected void onDestroy() {
        // Activity销毁时，提示系统回收
        // System.gc();
        ActivityUtil.getInstance().removeActivity(this);     // 移除Activity
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Resources resources = this.getResources();
        Configuration configuration = resources.getConfiguration();
        configuration.fontScale = 1;
        resources.updateConfiguration(configuration, resources.getDisplayMetrics());//字体的大小
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 点击手机上的返回键，返回上一层
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            // 移除Activity
            ActivityUtil.getInstance().removeActivity(this);
        }
        return super.onKeyDown(keyCode, event);
    }


    protected void initView() {
        // 隐藏标题栏
        if (getSupportActionBar() != null) getSupportActionBar().hide();
        ScreenManager screenManager = ScreenManager.getInstance();
        screenManager.setStatusBar(isStatusBar, this);
        screenManager.setScreenRoate(isScreenPortrait, this);
        screenManager.setFullScreen(isFullScreen, this);


        //沉侵式状态栏
        ImmersionBar immersionBar = ImmersionBar.with(this);
        immersionBar.statusBarDarkFont(true);
        immersionBar.init();


        AlphaAnimation alphaAnimation = new AlphaAnimation(0.3f, 1.0f);
        alphaAnimation.setDuration(2000);
        layoutWelcome.startAnimation(alphaAnimation);
        alphaAnimation.setAnimationListener(new AnimationListener());


    }


    private class AnimationListener implements Animation.AnimationListener {
        @Override
        public void onAnimationStart(Animation animation) {
            new Thread(new MyRunnable()).start();
        }

        @Override
        public void onAnimationEnd(Animation animation) {

        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    }

    private class MyRunnable implements Runnable {
        @Override
        public void run() {
            // 动画结束后，等待一会，再进行页面跳转
            SystemClock.sleep(ANIMATION_DELAY);
            String token = SPStaticUtils.getString(SPTools.token);
            if (!StringUtils.isEmpty(token)) {
                IntentUtils.getInstance().readyGoThenKill(WelcomeActivity.this, MainActivity.class);
            } else {
                IntentUtils.getInstance().readyGoThenKill(WelcomeActivity.this, LoginActivity.class);
            }

        }
    }


    /**
     * 初始化屏幕适配
     */
    protected void initScreenAdaption() {
        if (ScreenUtils.isPortrait()) {
            if (DeviceUtils.isTablet()) {
                AdaptScreenUtils.adaptWidth(BaseApplication.getBaseApplication().getResources(), AppConfig.widthInPxTable);
            } else {
                ScreenUtils.adaptScreen4VerticalSlide(this, AppConfig.widthInPx);
            }

        } else {
            if (ScreenUtils.isTablet()) {
                AdaptScreenUtils.adaptHeight(BaseApplication.getBaseApplication().getResources(), AppConfig.heightInPxTable);
            } else {
                ScreenUtils.adaptScreen4HorizontalSlide(this, AppConfig.heightInPx);
            }
        }
    }

}
